package frame.networks.tables;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import model.components.networks.network.VirtualNetwork;
import frame.MopraFrame;
import frame.networks.viewers.VirtualNetworksVisualizationViewersTabbedPane;

@SuppressWarnings("serial")
public class VirtualNetworksTable extends JTable implements MouseListener
{
	private List< VirtualNetwork > virtualNetworks;
	public List< VirtualNetwork > getVirtualNetworks(){ return virtualNetworks; }
	public void setVirtualNetworks( List< VirtualNetwork > virtualNetworks ){ this.virtualNetworks = virtualNetworks; }
	
	private MopraFrame mopraFrame;
	public MopraFrame getFrame(){ return mopraFrame; }
	public void setFrame( MopraFrame mopraFrame ){ this.mopraFrame = mopraFrame; }
	
	public VirtualNetworksTable( List< VirtualNetwork > virtualNetworks, MopraFrame mopraFrame )
	{
		this.setVirtualNetworks( virtualNetworks );
		this.setFrame( mopraFrame );
		
		VirtualNetworksTableModel virtualNetworksTableModel = new VirtualNetworksTableModel( new String[]{ "Networks", "Start", "End", "State" }, 0, virtualNetworks );
		
		for( VirtualNetwork virtualNetwork: this.getVirtualNetworks() )
			virtualNetworksTableModel.addRow( new Object[]{ virtualNetwork.getName(), virtualNetwork.getStartingDate(), virtualNetwork.getEndingDate(), virtualNetwork.getState() } );
		
		this.setModel( virtualNetworksTableModel );
		this.setDefaultRenderer( Object.class, new TableCellRenderer() );
		this.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		this.addMouseListener( this );
	}
	
	@Override
	public void mouseClicked( MouseEvent mouseEvent )
	{
		int selectedRowIndex = ( ( JTable )mouseEvent.getSource() ).getSelectedRow();
		String virtualNetworkName = ( String )this.getModel().getValueAt( selectedRowIndex, 0 );
		
		for( VirtualNetwork virtualNetwork: this.getVirtualNetworks() )
			if( virtualNetwork.getName().equals( virtualNetworkName ) )
			{
				VirtualNetworksVisualizationViewersTabbedPane virtualNetworksVisualizationViewersTabbedPane = this.getFrame().getNetworksPanel().getVirtualNetworksVisualizationViewersTabbedPane();
				virtualNetworksVisualizationViewersTabbedPane.setSelectedIndex( virtualNetworksVisualizationViewersTabbedPane.indexOfTab( virtualNetwork.getName() ) );
				break;
			}
	}
	@Override
	public void mouseReleased( MouseEvent mouseEvent ){}
	@Override
	public void mouseEntered( MouseEvent mouseEvent ){}
	@Override
	public void mouseExited( MouseEvent mouseEvent ){}
	@Override
	public void mousePressed( MouseEvent mouseEvent ){}

}
